package com.mls.fivedistrict.controller.portal;


import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.NewsFlashLikeRecord;
import com.mls.fivedistrict.server.NewsFlashLikeRecordService;
import com.mls.fivedistrict.server.NewsFlashService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Five Team
 * @since 2018-06-07
 */
@RestController
@RequestMapping("/api/app")
public class NewsFlashLikeRecordController extends BaseController {
    @Autowired
    NewsFlashLikeRecordService newsFlashLikeRecordService;

    @Autowired
    private NewsFlashService newsFlashService;

    /**
     * 新闻点赞接口
     *
     * @param newsFlashLikeRecord
     * @return
     */
    @PostMapping("/v1/newsFlashLikeRecord")
    public ServiceResponse addNewsFlashLikeRecord(@RequestBody NewsFlashLikeRecord newsFlashLikeRecord) {
        BeanValidator.check(newsFlashLikeRecord);
        newsFlashLikeRecord.setUserId(getUser().getId());
        Long newsFlashId = newsFlashLikeRecord.getNewsFlashId();
        boolean insert = newsFlashLikeRecordService.addNewsFlashLikeRecord(newsFlashLikeRecord);
        boolean update = newsFlashService.addLikeCount(newsFlashId);
        boolean flag = insert && update;
        if (flag) {
            return ServiceResponse.createSuccessResponse("快讯点赞成功!");
        } else {
            if (update) {
                newsFlashService.cutLikeCount(newsFlashId);
            }
            return ServiceResponse.createErrorResponse("快讯点赞失败");
        }
    }

    /**
     * 取消点赞接口
     *
     * @param newsFlashLikeRecordId
     * @return
     */
    @DeleteMapping("/v1/newsFlashLikeRecord/{newsFlashLikeRecordId}")
    public ServiceResponse deleteNewsFlashLikeRecord(@PathVariable Long newsFlashLikeRecordId) {
//        Optional.ofNullable(newsFlashLikeRecordId).orElseThrow(() -> new CustomerException("点赞的id不能为空"));
//        Wrapper wrapper = Condition.create();
//        wrapper.eq("user_id", getUser().getId());
//        wrapper.eq("id", newsFlashLikeRecordId);
//        NewsFlashLikeRecord newsFlashLikeRecord = newsFlashLikeRecordService.selectOne(wrapper);
//        Optional.ofNullable(newsFlashLikeRecord).orElseThrow(() -> new CustomerException("查无此条点赞记录"));
//        Long newsFlashId = newsFlashLikeRecord.getNewsFlashId();
//        newsFlashLikeRecordService.deleteById(newsFlashLikeRecord);
//        boolean update = newsFlashService.cutLikeCount(newsFlashId);
//        if (update) {
//            return ServiceResponse.createSuccessResponse("取消点赞成功!");
//        } else {
//            return ServiceResponse.createErrorResponse("发生未知异常，取消点赞失败");
//        }
        return null;
    }

}

